package com.qining618.cms.cms.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qining618.cms.cms.controller.admin.request.AdminChannelEditRequest;
import com.qining618.cms.cms.domain.Channel;
import com.qining618.cms.cms.service.ChannelService;
import com.qining618.common.annotation.Access;
import com.qining618.common.result.ApiResult;
import com.qining618.common.result.TableData;
import com.qining618.common.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * CMS栏目表 前端控制器
 * </p>
 *
 * @author qining
 * @date 2019-09-17
 */
@RestController
@RequestMapping("admin/api/cms/channel")
public class AdminChannelController {

    @Autowired
    private ChannelService channelService;

    @Access(needAdmin = true)
    @GetMapping(value = "page")
    @Description("分页")
    public ApiResult page(@RequestParam Map<String, Object> params) {
        Query<Channel> query = new Query<>(params);

        IPage<Channel> pageInfo = channelService.page(query.getPage(), query.getQueryWrapper());

        return ApiResult.ok(TableData.of(pageInfo));
    }

    @Access(needAdmin = true)
    @PostMapping
    @Description("添加")
    public ApiResult add(@RequestBody @Validated AdminChannelEditRequest channelEditRequest) {
        Channel channel = channelService.add(channelEditRequest.getName(), channelEditRequest.getLink(),
                channelEditRequest.getPriority(), channelEditRequest.getIsDisplay());
        return ApiResult.ok(channel);
    }

    @Access(needAdmin = true)
    @PutMapping(value = "{id}")
    @Description("编辑")
    public ApiResult edit(
            @PathVariable(value = "id") Long id,
            @RequestBody @Validated AdminChannelEditRequest channelEditRequest) {
        Channel channel = channelService.edit(id, channelEditRequest.getName(), channelEditRequest.getLink(),
                channelEditRequest.getPriority(), channelEditRequest.getIsDisplay());
        return ApiResult.ok(channel);
    }

    @Access(needAdmin = true)
    @DeleteMapping(value = "{id}")
    @Description("删除")
    public ApiResult delete(@PathVariable(value = "id") Long id) {
        channelService.deleteById(id);
        return ApiResult.ok();
    }

}

